package com.hd.assistant.dal.dao.ibatis.ordermining;

import java.util.List;

import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.orderming.TcOrderDAO;
import com.hd.assistant.dal.dao.query.ordermining.TcOrderQuery;
import com.hd.assistant.dal.dataobject.ordermining.Reports;
import com.hd.assistant.dal.dataobject.ordermining.TcOrder;
import com.taobao.common.dao.persistence.DBRoute;
import com.taobao.common.dao.persistence.SqlMapBaseDAO;

public class IbatisTcOrderDao extends SqlMapBaseDAO implements TcOrderDAO {

	@Override
	public void insertTOrder(TcOrder order) throws DAOException {
		if (order == null)
			throw new DAOException("order is null");
		try {
			this.executeInsert("Orders.insertOrderDO", order,
					DBRoute.getOrderRoute());
		} catch (Exception e) {
			throw new DAOException("error insert order", e);
		}
	}

	public TcOrder getTOrderByTid(Long tid) throws DAOException {
		if (tid == null)
			throw new DAOException("tid is null");
		try {
			return (TcOrder) this
					.executeQueryForObject("Orders.selectOrdersDOsByTid", tid,
							DBRoute.getOrderRoute());
		} catch (Exception e) {
			throw new DAOException("error getTOrderByTid order tid:" + tid, e);
		}
	}

	public int updateTOrder(TcOrder update) throws DAOException {
		if (update == null)
			throw new DAOException("update is null");
		try {
			return this.executeUpdate("Orders.updateOrdersDO", update,
					DBRoute.getOrderRoute());
		} catch (Exception e) {
			throw new DAOException("error executeUpdate order tid:"
					+ update.getTid(), e);
		}
	}

	@Override
	public TcOrderQuery getTOrdersByQuery(TcOrderQuery query)
			throws DAOException {
		if (query == null || query.getUserId() == null) {
			throw new DAOException("query is null || query.getUserId() == nul");
		}
		try {
			int totalCount = 0;

			// 获取当前条件下 记录数
			totalCount = (Integer) this.executeQueryForObject(
					"Orders.selectCountsByQuery", query,
					DBRoute.getOrderRoute());
			query.setTotalCount(totalCount);

			@SuppressWarnings("unchecked")
			List<TcOrder> tcOrders = (List<TcOrder>) this.executeQueryForList(
					"Orders.selecOrdersDOsByQuery", query,
					DBRoute.getOrderRoute());
			query.setTcOrders(tcOrders);
		} catch (Exception e) {
			throw new DAOException("IbatisTcOrderDao.getTOrdersByQuery", e);
		}

		return query;
	}

	@Override
	public List<Reports> getReportsByUserId(Long userId,Integer type, String dateFrom,
			String dateTo) throws DAOException {
		if(userId == null)
			throw new DAOException("userId == nul");
		if(type == null)
			type = Reports.TYPE_PAYDATE;
		
		Reports query = new Reports();
		query.setType(type);
		query.setUserId(userId);
		query.setDateFrom(dateFrom);
		query.setDateTo(dateTo);
		
		try {
			@SuppressWarnings("unchecked")
			List<Reports> reports = (List<Reports>) this.executeQueryForList(
					"Orders.selectReports", query,
					DBRoute.getOrderRoute());
			return reports;
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisTcOrderDao.getReportsByUserId userid = "+userId, e);
		}
		
	}

	@Override
	public List<Reports> getBuyerReportsByUserId(Long userId, Integer tradeFrom)
			throws DAOException {
		if(userId == null)
			throw new DAOException("userId == nul");
	
		
		Reports query = new Reports();
		query.setUserId(userId);
		query.setTradeFrom(tradeFrom);
		
		try {
			@SuppressWarnings("unchecked")
			List<Reports> reports = (List<Reports>) this.executeQueryForList(
					"Orders.selectBuyerReports", query,
					DBRoute.getOrderRoute());
			return reports;
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisTcOrderDao.getBuyerReportsByUserId userid = "+userId, e);
		}
	}

	
}
